package com.btpj.module_search.ui

import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import com.btpj.jetpack.base.BaseFragment
import com.btpj.module_search.R
import com.btpj.module_search.adapter.HotKeyListAdapter
import com.btpj.module_search.databinding.FragmentHotKeyBinding
import kotlinx.android.synthetic.main.fragment_hot_key.*

class HotKeyFragment : BaseFragment<HotKeyViewModel, FragmentHotKeyBinding>() {

    private val mAdapter by lazy { HotKeyListAdapter() }

    companion object {
        fun newInstance() = HotKeyFragment()
    }

    override fun getLayoutId() = R.layout.fragment_hot_key

    override fun initView() {
        recycleView.apply {
            layoutManager = GridLayoutManager(requireContext(), 2)
            adapter = mAdapter
        }
        mAdapter.setOnItemClickListener { _, _, position ->
            (requireActivity() as SearchActivity).setHotKeyInputSearch(mAdapter.getItem(position).name)
        }
    }

    override fun createObserve() {
        super.createObserve()
        mViewModel.hotKeyLiveData.observe(viewLifecycleOwner, Observer {
            mAdapter.setList(it)
        })
    }

}